#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
文件格式转换工具启动脚本
"""

import sys
import os
from pathlib import Path

# 添加当前目录到Python路径
current_dir = Path(__file__).parent
sys.path.insert(0, str(current_dir))

try:
    from dependency_checker import DependencyChecker
    from main import main as run_main
except ImportError as e:
    print(f"导入模块失败: {e}")
    print("请确保所有文件都在同一目录下")
    sys.exit(1)


def check_and_run():
    """检查依赖并运行应用程序"""
    print("文件格式转换工具 (纯Python实现)")
    print("=" * 40)
    
    # 检查依赖
    checker = DependencyChecker()
    
    # 快速检查关键依赖
    try:
        import PyQt6
        print("✓ PyQt6已安装")
    except ImportError:
        print("✗ PyQt6未安装")
        print("正在尝试安装PyQt6...")
        try:
            import subprocess
            result = subprocess.run([sys.executable, "-m", "pip", "install", "PyQt6"], 
                                  capture_output=True, text=True)
            if result.returncode == 0:
                print("✓ PyQt6安装成功")
            else:
                print("✗ PyQt6安装失败")
                print("请手动运行: pip install PyQt6")
                return False
        except Exception as e:
            print(f"安装PyQt6时出错: {e}")
            return False
    
    print("\n注意：本工具已完全使用Python库实现文件转换")
    print("无需安装LibreOffice、Calibre等外部工具")
    print("\n启动应用程序...")
    
    try:
        # 运行主程序
        run_main()
    except Exception as e:
        print(f"启动应用程序时出错: {e}")
        print("\n运行完整依赖检查...")
        checker.run_full_check()
        return False
    
    return True


if __name__ == "__main__":
    try:
        check_and_run()
    except KeyboardInterrupt:
        print("\n程序被用户中断")
    except Exception as e:
        print(f"程序运行出错: {e}")
        print("\n请运行dependency_checker.py检查依赖")